package org.goldenport.g3.messages

import org.goldenport.g3.events.RestEvent

/**
 * @since   Jul.  2, 2011
 * @version Jul.  2, 2011
 * @author  ASAMI, Tomoharu
 */
abstract class ResulterBase extends PartialFunction[AnyRef, AnyRef] {
  protected def is_Target(r: AnyRef): Boolean
  protected def get_Target(r: AnyRef): AnyRef

  override def isDefinedAt(in: AnyRef): Boolean = {
    in match {
      case r: RestCommand => r.content match {
        case Some(r2) => is_Target(r2) 
        case None => false  
      }
      case r: RestEvent => r.content match {
        case Some(r2) => is_Target(r2)
        case None => false
      }
      case _ => is_Target(in)
    }
  }

  override def apply(in: AnyRef): AnyRef = {
    in match {
      case r: RestCommand => r.content match {
        case Some(r2) => get_Target(r2) 
        case None => throw new MatchError()
      }
      case r: RestEvent => r.content match {
        case Some(r2) => get_Target(r2)
        case None => throw new MatchError()
      }
      case _ => get_Target(in)
    }
  }
}